<div class="row">

    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
        <div class="widget am-cf">
            <div class="widget-head am-cf">
                <div class="widget-title am-fl">
                    授权
                </div>
                <div class="widget-function am-fr">
                </div>
            </div>
            <div class="widget-body am-fr">

                <form id="formUserAuthor" class="am-form tpl-form-line-form">
                    <input type="hidden" id="id" name="id" value="${userAuthor.id!''}">

                    <div class="am-form-group">
                        <label for="user-name" class="am-u-sm-3 am-form-label">姓名 <span class="tpl-form-line-small-title"></span></label>
                        <div class="am-u-sm-9">
                            <input type="text" disabled value="${userAuthor.name!''}">
                        </div>
                    </div>

                    <div class="am-form-group">
                        <label for="user-name" class="am-u-sm-3 am-form-label">用户登录名 <span class="tpl-form-line-small-title"></span></label>
                        <div class="am-u-sm-9">
                            <input type="text" disabled value="${userAuthor.logName!''}">
                        </div>
                    </div>

                    <div class="am-form-group">
                        <label for="user-name" class="am-u-sm-3 am-form-label">用户拥有角色 <span class="tpl-form-line-small-title"></span></label>
                        <div class="am-u-sm-9">
                            <#if userByRole??>
                                <#list userByRole as ur>
                                    <input checked type="checkbox" disabled="disabled"  value="${ur.id!''}" > ${ur.name!''}
                                    <input type="button" class="am-btn am-btn-warning am-btn-sm" value="删除" onclick="delUserRole('${ur.id!''}')"/>
                                    <br/>
                                </#list>
                            </#if>
                        </div>
                    </div>

                    <div class="am-form-group">
                        <label for="user-name" class="am-u-sm-3 am-form-label">可赋予角色 <span class="tpl-form-line-small-title"></span></label>
                        <div class="am-u-sm-9">
                            <#if userByRoleCreatRole??>
                                <#list userByRoleCreatRole as ur>
                                    <#if ur??>
                                        <input type="checkbox" name="roleArr"  value="${ur.id!''}" > ${ur.name!''} &nbsp;&nbsp;
                                        <input type="button" class="am-btn am-btn-warning am-btn-sm" value="权限详情" onclick="showAuthor('${ur.id!''}')" />
                                        <br/>
                                    </#if>
                                </#list>
                            </#if>
                        </div>
                    </div>



                    <div class="am-form-group">
                        <div class="am-u-sm-9 am-u-sm-push-3">
                            <button type="button" class="am-btn am-btn-primary tpl-btn-bg-color-success " onclick="addUserAuthor()">赋予角色</button>
                            <button type="button" class="am-btn am-btn-primary tpl-btn-bg-color-success " onclick="javascrtpt:window.location.href='/main/indexUrl.php'">返回首页</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>





</div>


<script type="text/javascript">

    /**
     * 展示角色的权限
     */
    function showAuthor(id) {
        if (checkNull(id)){
            alert("权限ID不为空");
            return;
        }
        var url = "${request.contextPath}/author/showAuthor.php";
        $.ajax({
            url: url,
            type:"post",
            dataType:"json",
            data:{'roleId' : id},
            success:function(data){
                if (data.status == "success"){
                    layer.alert(data.msg, {
                        icon: 1,
                        skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
                    })

                } else if (data.status == 'error'){
                    alert(data.msg);
                } else {
                    alert("未知情况");
                }
            },
            error:function(e){
                alert("错误！！");
            }
        });


    }

    /**
     * 删除用户拥有的角色
     */
    function delUserRole(id) {
        var uid = $("#id").val();
        if (checkNull(uid)){
            alert("没有找到用户");
            return;
        }
        var url = "${request.contextPath}/author/delUserRole.php";
        $.ajax({
            url: url,
            type:"post",
            dataType:"json",
            data:{'id' : id, "uid" : uid},
            success:function(data){
                if (data.status == "success"){
                    alert(data.msg);
                    window.location.reload(); //刷新当前页面.
                } else if (data.status == 'error'){
                    alert(data.msg);
                } else {
                    alert("未知情况");
                }
            },
            error:function(e){
                alert("错误！！");
            }
        });
    }

    /**
     * 用户授权
     */
    function addUserAuthor() {
        if (!checkRole()){
            return;
        }

        var url = "${request.contextPath}/author/addUserAuthor.php";
        var formUserAuthor = $("#formUserAuthor").serialize();
        $.ajax({
            url: url,
            type:"post",
            dataType:"json",
            data:formUserAuthor,
            success:function(data){
                if (data.status == "success"){
                    alert(data.msg);
                    window.location.reload(); //刷新当前页面.
                } else if (data.status == 'error'){
                    alert(data.msg);
                } else {
                    alert("未知情况");
                }
            },
            error:function(e){
                alert("错误！！");
            }
        });
    }

    /**
     * 检查数据是否填录完整
     * 返回 false， 则数据未完整
     * 返回 true， 则数据正常
     */
    function checkRole() {
        if(!$("input[name='roleArr']").is(':checked')) {
            alert("请选择授予的角色");
            return false;
        }

        var id = $("#id").val();
        if (checkNull(id)){
            alert("参数异常，请刷新后再试！");
            return false;
        }

        return true;
    }


</script>